package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 创建会员
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Member create(Member member) {
        // 根据phoneNumber查询是否是会员
        Member people = memberDao.select(member.getPhoneNumber());

        //如果是会员的话，就更新
        if (people != null) {
            member.setId(people.getId());
            memberDao.update(member);
        }
        //如果不是会员的话就新增插入
        else {
          member.setRegTime(new Date());
          memberDao.insert(member);
        }
        return memberDao.selectById(member.getId());
    }


}
